+function($, window){
    'use strict';
    var app = {
        name: 'Bolt',
        version: '1.0.0'
    };

    app.defaults = {
        menubar: {
            folded: false,
            theme: 'light',
            themes: ['light', 'dark']
        },
        navbar: {
            theme: 'primary',
            themes: ['primary', 'success', 'warning', 'danger', 'pink', 'purple', 'inverse', 'dark']
        },
        contabs:{
            index:'welcome'
        }
    };

    // Cache DOM
    app.$body = $('body');
    app.$menubar = $('.site-menubar');
    app.$appMenu = app.$menubar.find('.site-menu').first();
    app.$navbar = $('#app-navbar');
    app.$main = $('#app-main');
    app.$contabs = $('#admui-siteConTabs');
    app.$signOut = $('#admin-signOut')
    app.$fullscreen = $('#admin-Fullscreen')


    // Which is the original loaded layout ?
    app.defaultLayout = app.$body.hasClass('menubar-left');
    app.topbarLayout = app.$body.hasClass('menubar-top');

    // TODO
    app.settings = app.defaults;

    var appSettings = app.name+app.version+"Settings";
    app.storage = $.localStorage;

    if (app.storage.isEmpty(appSettings)) {
        app.storage.set(appSettings, app.settings);
    }
    else {
        app.settings = app.storage.get(appSettings);
    }

    app.saveSettings = function() {
        app.storage.set(appSettings, app.settings);
    };

    // initialize navbar
    //app.$navbar.removeClass('primary').addClass(app.settings.navbar.theme).addClass('in');
    //app.$body.removeClass('theme-primary').addClass('theme-'+app.settings.navbar.theme);

    // initialize menubar
   //app.$menubar.removeClass('light').addClass(app.settings.menubar.theme).addClass('in');
   // app.$body.removeClass('menubar-light').addClass('menubar-'+app.settings.menubar.theme);

    // initialize main
   // app.$main.addClass('in');


    window.app = app;
}(jQuery, window);


// menubar MODULE
// =====================

+function($, window){
    'use strict';
    // Cache DOM
    var $body = app.$body,
        $menubar = app.$menubar,
        $appMenu = app.$appMenu,
        $menubarFoldButton = $('#bolt-toggleMenubar'),
        $menubarToggleButton = $('#menubar-toggle-btn'),
        $signOutButton = app.$signOut,
        $fullscreenButton = app.$fullscreen;

    // menubar object
    var menubar = {
        open: false,
        folded: app.settings.menubar.folded,
        scrollInitialized: false,

        init: function() {
           // app.defaultLayout && this.folded && this.fold();
            this.listenForEvents();
        },

        listenForEvents: function() {
            var self = this;
            $(window).on('load', function(e) {
                var current = Breakpoints.current();
                 self.initScroll();

                if (current.name === 'xs') {
                    // $('body').addClass('site-menubar-xs');
                    // push the menubar out
                    self.pushOut();

                    // if the menubar is folded then unfold it
                    !self.folded && self.unFold();
                }

            });
            self.readjustScroll();
            self.toggleScroll();
            $('#bolt-toggleMenubar,button[data-toggle="menubar"]').on('click', function(e){
                !self.folded ? self.fold() : self.unFold();
                e.preventDefault();
            });

            // showing and hiding the menubar
            $menubarToggleButton.on('click', function(e){
                self.open ? self.pushOut() : self.pullIn();
                e.preventDefault();
            });

            $signOutButton.on('click',function(e){
                self.logout()
                e.preventDefault();
            })

            $fullscreenButton.on('click',function (e) {
                self.fullscreen();
                e.preventDefault();
            })



           // toggling submenus when the menubar is folded   折叠状态以及菜单hover操作
           // $(document).on('mouseenter mouseleave', 'body.site-menubar-fold ul.site-menu > li.has-sub', function(e){
           $(document).on('mouseenter mouseleave', 'body.site-menubar-fold ul.site-menu > li.has-sub', function(e){

               $(this).toggleClass('hover').siblings('li').removeClass('hover');
               $(this).find("> ul.site-menu-sub").fadeIn();
               $(this).siblings().find("> ul.site-menu-sub").fadeOut();
               $('body').toggleClass('site-menubar-fold-hover');
               //

           });
            //xs手机状态  菜单和over操作
           $(document).on('mouseenter mouseleave', 'body.site-menubar-unfold ul.site-menu > li.has-sub', function(e){

               $(this).toggleClass('hover').siblings('li').removeClass('hover');

               if(Breakpoints.current().name === 'xs'){
                   $(this).find("> ul.site-menu-sub").fadeIn();
                   $(this).siblings().find("> ul.site-menu-sub").fadeOut();
               }

                $('body').toggleClass('site-menubar-fold-hover');
            });

            // // toggling submenus in the (topbar) layout
            $(document).on('mouseenter mouseleave', 'body.menubar-top ul.app-menu li.has-submenu', self.toggleTopbarSubmneuOnHover);


            // toggling submenus on click    点击一级菜单打开二级菜单
            $(document).on('click', 'body.site-menubar-unfold .site-menu .submenu-toggle, body.menubar-fold .app-menu .submenu .submenu-toggle', self.toggleSubmenuOnClick);

            //点击二级菜单
            $(document).on('click','.site-menu-item.has-sub.open ul.site-menu-sub>li.site-menu-item.is-shown,.site-menu-item.has-sub.hover ul.site-menu-sub>li.site-menu-item.is-shown',function (e) {
                // this.listNum = $(this).index();

                if(!self.folded){
                    $(this).addClass('hover').siblings().removeClass('hover');
                    $(this).parents('.site-menu-item.has-sub').siblings().find("li.site-menu-item").removeClass('hover');
                    $(this).parents('.site-menu-item.has-sub').addClass('selected').siblings().removeClass('selected');

                }else{
                    $(this).addClass('hover').siblings().removeClass('hover');
                    $(this).parents('.site-menu-item.has-sub').addClass('selected').siblings().removeClass('selected');
                }
                if(Breakpoints.current().name === 'xs'){

                    $(this).parent().find('> .site-menu-sub')
                        .slideToggle(200)

                }
                e.preventDefault();
            });

            // readjust the scroll height on resize and orientationchange
            $(window).on('resize orientationchange', function(){
                self.readjustScroll();
                self.toggleScroll();
            });

        },

        logout:function () {
           Bolt.confirm("确认注销登录吗？", function(r){
                if (r){
                    window.location.href = "/logout"
                }
            });
        },

        fullscreen:function(){
            if (!document.fullscreenElement && !document.mozFullScreenElement && !document.webkitFullscreenElement) {
                if (document.documentElement.requestFullscreen) {
                    document.documentElement.requestFullscreen()
                } else {
                    if (document.documentElement.mozRequestFullScreen) {
                        document.documentElement.mozRequestFullScreen()
                    } else {
                        if (document.documentElement.webkitRequestFullscreen) {
                            document.documentElement
                                .webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT)
                        }
                    }
                }
            } else {
                if (document.cancelFullScreen) {
                    document.cancelFullScreen()
                } else {
                    if (document.mozCancelFullScreen) {
                        document.mozCancelFullScreen()
                    } else {
                        if (document.webkitCancelFullScreen) {
                            document.webkitCancelFullScreen()
                        }
                    }
                }
            }
        },

        setDefaultLayout: function() {
            app.$body.removeClass('menubar-top').addClass('menubar-left menubar-unfold');
            return true;
        },

        setTopbarLayout: function() {
            app.$body.removeClass('menubar-left menubar-unfold menubar-fold').addClass('menubar-top');
            return true;
        },

        cloneAppUser: function() {
            var $navbarCollapse = $('.navbar .navbar-collapse');
            if ($navbarCollapse.find('.app-user').length == 0){
                $menubar.find('.app-user').clone().appendTo($navbarCollapse);
            }
            return true;
        },
        //设置头像部分的功能
        foldAppUser: function() {
            $('.app-user .avatar').addClass('dropdown').find('>a').attr('data-toggle', 'dropdown');
            $('.app-user .dropdown-menu').first().clone().appendTo('.app-user .avatar')
            return true;
        },

        reduceAppMenu: function(){
            var $appMenu = $('body.menubar-top .app-menu');
            // if the menu is already customized return true
            if ($appMenu.find('>li.more-items-li').length) return true;

            var $menuItems = $appMenu.find('> li:not(.menu-separator)');
            if ($menuItems.length > 5) {
                var $moreItemsLi = $('<li class="more-items-li has-submenu"></li>'),
                    $moreItemsUl = $('<ul class="submenu"></ul>'),
                    $moreItemsToggle = $('<a href="javascript:void(0)" class="submenu-toggle"></a>');
                    $moreItemsToggle.append(['<i class="menu-icon zmdi zmdi-more-vert zmdi-hc-lg"></i>',
                    '<span class="menu-text">More...</span>', '<i class="menu-caret zmdi zmdi-hc-sm zmdi-chevron-right"></i>']);

                $menuItems.each(function(i, item){
                    if (i >= 5) $(item).clone().appendTo($moreItemsUl);
                });

                $moreItemsLi.append([$moreItemsToggle, $moreItemsUl]).insertAfter($appMenu.find('>li:nth-child(5)'));
            }

            $(document).trigger('app-menu.reduce.done');

            return true;
        },
        //点击一级菜单  添加open
        toggleSubmenuOnClick: function(e) {
            // this.indexNum=$(this).parent().index();
            $(this).parent().toggleClass('open selected').find('> .site-menu-sub')
                .slideToggle(200).end().siblings()
                .removeClass('open selected').find('> .site-menu-sub')
                .slideUp(200);
            e.preventDefault();
        },

        toggleTopbarSubmneuOnHover: function(e){

            var $this = $(this), total = $this.offset().left + $this.width();
            var ww = $(window).width();
            if ((ww - total) < 220) {
                $this.find('> .submenu').css({left: 'auto', right: '100%'});
            } else if ((ww - total) >= 220 && !$this.is('.app-menu > li')) {
                $this.find('> .submenu').css({left: '100%', right: 'auto'});
            }

                $this.find('> .submenu').css({left: '100%', right: 'auto'});

            $(this).toggleClass('open').siblings().removeClass('open');
        },

        fold: function() {
            $('body').removeClass('site-menubar-unfold').addClass('site-menubar-fold');
            $menubarFoldButton.removeClass('is-active');
            this.toggleScroll() && this.toggleMenuHeading() && (this.folded = true);

            $('.site-menu').find('li.open').removeClass('open').addClass('selected')&& $appMenu.find('.submenu').slideUp();
            return true;
        },

        unFold: function() {
            $('body').removeClass('site-menubar-fold').addClass('site-menubar-unfold');
            $menubarFoldButton.addClass('is-active');
            // initialize the scroll if it's not initialized
            this.scrollInitialized || this.initScroll();
            this.toggleScroll() && this.toggleMenuHeading() && (this.folded = false);

            $appMenu.find('li.open').removeClass('open') && $appMenu.find('.submenu').slideUp();
            return true;
        },

        pullIn: function() {

            $body.addClass('menubar-in') && $menubarToggleButton.addClass('is-active') && (this.open = true);
            return true;
        },

        pushOut: function() {
            $body.removeClass('menubar-in') && $menubarToggleButton.removeClass('is-active') && (this.open = false);
            return true;
        },

        initScroll: function() {
            var $scrollInner = $('body.site-menubar-unfold .site-menu');
            if (!this.scrollInitialized) {
                $scrollInner.slimScroll({
                    height: 'auto',
                    position: 'right',
                    size: "5px",
                    color: '#98a6ad',
                    wheelStep: 10,
                    touchScrollStep: 50
                });
                this.scrollInitialized = true;
            }
            return true;
        },

        // //目录栏除去头像后的高度
        readjustScroll: function(e){
            var parentHeight = $menubar.height(),
                $targets = $('.tab-content'),
                avatarHeight;
            if (Breakpoints.current().name === 'xs') {
                if(!$('body').hasClass('site-menubar-xs')){
                    $('body').addClass('site-menubar-xs')
                }
            } else {
                $('body').removeClass('site-menubar-xs')
            }
            avatarHeight = $(".app-user").height();
            $targets.height(parentHeight - avatarHeight - 30);
            $appMenu.height(parentHeight - avatarHeight - 30);
        },

        //左侧目录滚动条
        toggleScroll: function(){
            var $scrollContainer = $('.tab-content');

            if(!$body.hasClass('site-menubar-unfold')){
                $scrollContainer.css('overflow', 'inherit').parent().css('overflow', 'inherit');
                $appMenu.css("overflow-y",'inherit');
            }
            else{
                if( !$body.hasClass('site-menubar-xs')){
                    $appMenu.css("overflow-y",'auto')
                }else{
                    $appMenu.css("overflow-y",'inherit')
                }
            }
            return true;
        },

        toggleMenuHeading: function() {

            if ($body.hasClass("menubar-fold")) {
                $('.app-menu > li:not(.menu-separator)').each(function(i, item){

                    if (!$(item).hasClass('has-submenu')) {
                        $(item).addClass('has-submenu').append('<ul class="submenu"></ul>');
                    }
                    var href = $(item).find('a:first-child').attr("href");
                    var menuHeading = $(item).find('> a > .menu-text').text();
                    $(item).find('.submenu').first().prepend('<li class="menu-heading"><a href="'+href+'">'+menuHeading+'</a></li>');
                });
            } else {
                $appMenu.find('.menu-heading').remove();

            }

            return true;
        },

        highlightOpenLink: function() {
            var currentPageName = location.pathname.slice(location.pathname.lastIndexOf('/') + 1),
                currentPageLink = $appMenu.find('a[href="'+currentPageName+'"]').first();

            currentPageLink.parents('li').addClass('active');

            if ($body.hasClass('menubar-left') && !this.folded) {
                currentPageLink.parents('.has-submenu').addClass('open').find('>.submenu').slideDown(500);
            }

            return true;
        },

        // gets the DOM applied theme
        getAppliedTheme: function() {
            var appliedTheme = "", themes = app.settings.menubar.themes, theme;
            for(theme in themes) {
                if ($menubar.hasClass(themes[theme])) {
                    appliedTheme = themes[theme];
                    break;
                }
            }
            return appliedTheme;
        },

        getCurrentTheme: function() {
            return app.settings.menubar.theme;
        },

        setTheme: function(theme) {
            if (theme) app.settings.menubar.theme = theme;
        },

        // applies the theme to the DOM
        applyTheme: function() {
            $body.removeClass('menubar-'+this.getAppliedTheme())
                .addClass('menubar-'+this.getCurrentTheme());
            $menubar.removeClass(this.getAppliedTheme())
                .addClass(this.getCurrentTheme());
        }
    };

    window.app.menubar = menubar;
}(jQuery, window);


//路径切换
+function($, window){
    'use strict';
    var $body = app.$body,
        $contabs = app.$menubar;

    // menubar object
    var contabs = {
        init: function() {
            if ($.support.pjax) {
                $(document).pjax('[data-pjax] a, a[data-pjax]','#admui-pageContent')
            }
        }
    }
   window.app.contabs = contabs;
}(jQuery, window);


// other    本项目没用到
+function($, window) { 'use strict';

    $(window).on('load resize orientationchange', function(){
        $('[data-toggle="tooltip"]').tooltip();
    });



}(jQuery, window);

//本项目未用到
+function($, window){
    'use strict';
    // asideSwitch object
    var asideSwitch = {
        init: function() {
            $(document).on('click.bolt.asideSwitch.data-api', '.page-aside-switch', function(e) {
                if($('.fa-chevron-left').is(':hidden')){
                    $('.page-aside').addClass('open');
                    $(this).find('.fa-chevron-left').show();
                    $(this).find('.fa-chevron-right').hide();
                }else{
                    $('.page-aside').removeClass('open');
                    $(this).find('.fa-chevron-left').hide();
                    $(this).find('.fa-chevron-right').show();
                }
                e.preventDefault();
            });
        }
    }

    window.app.asideSwitch = asideSwitch;

}(jQuery, window);




$(document).ready(function () {
    window.app.menubar.init();
});


